package com.zyf.pattern.template.dao;

import com.zyf.pattern.template.JdbcTemplate;
import com.zyf.pattern.template.entity.Member;
import com.zyf.pattern.template.entity.RowMapper;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 取消使用继承的方式
 * 解耦，将继承改成接口实现，即RowMpper，这样更MemberDao没有强耦合，也可以使用匿名类的方式
 * create by yifeng
 */
//public class MemberDao extends JdbcTemplate {
public class MemberDao {

    // 为什么不继承，主要是为了解耦，同时此处类似静态代理
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(null);
//
//    @Override
//    public Object processResult(ResultSet rs, int rowNum) throws SQLException {
//        Member member = new Member();
//
//        member.setUsername(rs.getString("username"));
//        member.setPasword(rs.getString("password"));
//        member.setAddr(rs.getString("addr"));
//        member.setAge(rs.getInt("age"));
//
//        return member;
//    }

    public List<?> query() {
        String sql = "select * from t_member";
//        return (List<Object>) super.executeQuery(sql, null);
        return jdbcTemplate.executeQuery(sql, new RowMapper<Object>() {

            @Override
            public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                Member member = new Member();

                member.setUsername(rs.getString("username"));
                member.setPasword(rs.getString("password"));
                member.setAddr(rs.getString("addr"));
                member.setAge(rs.getInt("age"));

                return member;
            }
        }, null);

    }


}
